package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.AdminGetDope;
import mdm.led.entity.Dope;
import mdm.led.entity.User;
import mdm.led.entity.department.Department;
import mdm.led.service.DopeService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月2日
 * @explain : 通知通报控制器 WEB
 */
@Controller
@RequestMapping("dopeWeb")
@Api(description = "通知通报 WEB")
public class DopeWebController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DopeWebController.class);

	@Autowired
	private DopeService dopeserviceImpl;

	@ApiOperation("管理员发送通知通报")
	@ResponseBody
	@RequestMapping(value = "adminSendDope", method = RequestMethod.POST)
	public AjaxResponse adminSendDope(@RequestBody Dope dope) {
		LOGGER.info("\n-------------- 管理员发送通知通报 ------------");

		int iRet = dopeserviceImpl.addAppointManDope(dope);

		LOGGER.info("\n-------------- 结果 :" + iRet + " ------------");
		if (iRet > 0) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "发送成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "发送失败");
		}
	}

	@ApiOperation("绑定发送对象下拉")
	@ResponseBody
	@RequestMapping(value = "bindObjectSelect", method = RequestMethod.GET)
	public AjaxResponse bindObjectSelect() {
		LOGGER.info("\n-------------- 绑定发送对象下拉 ------------");
		List<User> list = dopeserviceImpl.findAllDoUser();
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(list) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
	}

	@ApiOperation("绑定发送部门下拉")
	@ResponseBody
	@RequestMapping(value = "findAllDepartment", method = RequestMethod.GET)
	public AjaxResponse findAllDepartment() {
		LOGGER.info("\n-------------- 绑定发送部门下拉 ------------");
		List<Department> list = dopeserviceImpl.findAllDepartment();
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(list) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);

	}

	@ApiOperation("管理员查看历史通报")
	@ResponseBody
	@RequestMapping(value = "findHistoryDope", method = RequestMethod.GET)
	public AjaxResponse findHistoryDope(@RequestParam("pageSize") Integer pageSize,
			@RequestParam("currPage") Integer currPage) {
		LOGGER.info("\n-------------- 管理员查看历史通报 ------------");
		// 开启分页
		Page<AdminGetDope> page = PageHelper.startPage(currPage, pageSize);
		List<AdminGetDope> list = dopeserviceImpl.findHistoryDope();
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", list);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", list.size());
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(data) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

}
